package com.jade.journal.impl;

import java.util.List;

import com.jade.framework.base.util.Parameters;
import com.jade.journal.*;

/**
 * journal实现。
 *
 * @author Daisy
 * @version 1.0 14-9-21 下午8:56
 */
public class SimpleJournal
        implements Journal
{
    private String name;
    private JournalLevel level = JournalLevel.INFO;

    public SimpleJournal(String name)
    {
        this.name = name;
    }

    public String getName()
    {
        return name;
    }

    private boolean isLevel(JournalLevel level)
    {
        return this.level.ordinal() >= level.ordinal();
    }

    public boolean isStat()
    {
        return isLevel(JournalLevel.STAT);
    }

    public boolean isInfo()
    {
        return isLevel(JournalLevel.INFO);
    }

    public boolean isWarn()
    {
        return isLevel(JournalLevel.WARN);
    }

    public boolean isNotice()
    {
        return isLevel(JournalLevel.NOTICE);
    }

    public boolean isError()
    {
        return isLevel(JournalLevel.ERROR);
    }

    public boolean isFatal()
    {
        return isLevel(JournalLevel.FATAL);
    }

    public void stat(Object... param)
    {
        journal(JournalLevel.STAT, param);
    }

    public void info(Object... param)
    {
        journal(JournalLevel.INFO, param);
    }

    public void warn(Object... param)
    {
        journal(JournalLevel.WARN, param);
    }

    public void notice(Object... param)
    {
        journal(JournalLevel.NOTICE, param);
    }

    public void error(Object... param)
    {
        journal(JournalLevel.ERROR, param);
    }

    public void fatal(Object... param)
    {
        journal(JournalLevel.FATAL, param);
    }

    public void statEntity(JournalEntity entity)
    {
        journalEntity(JournalLevel.STAT, entity);
    }

    public void infoEntity(JournalEntity entity)
    {
        journalEntity(JournalLevel.INFO, entity);
    }

    public void warnEntity(JournalEntity entity)
    {
        journalEntity(JournalLevel.WARN, entity);
    }

    public void noticeEntity(JournalEntity entity)
    {
        journalEntity(JournalLevel.NOTICE, entity);
    }

    public void errorEntity(JournalEntity entity)
    {
        journalEntity(JournalLevel.ERROR, entity);
    }

    public void fatalEntity(JournalEntity entity)
    {
        journalEntity(JournalLevel.FATAL, entity);
    }

    private JournalHandler handler;

    public JournalHandler getHandler()
    {
        return handler;
    }

    public void setHandler(JournalHandler handler)
    {
        this.handler = handler;
    }

    public JournalLevel getLevel()
    {
        return level;
    }

    public void setLevel(JournalLevel level)
    {
        this.level = level;
    }

    public void journal(JournalLevel level, Object... objects)
    {
        if (objects != null && objects.length > 0) {
            JournalEvent event = new JournalEvent();
            event.setLevel(level);
            event.setTime(System.currentTimeMillis());
            event.setName(name);
            if (objects.length == 1 && (objects[0] instanceof Parameters || objects[0] instanceof List)) {
                event.setItem(objects[0]);
            }
            else {
                event.setItem(objects);
            }
            getHandler().handle(event);
        }
    }

    public void journalEntity(JournalLevel level, JournalEntity entity)
    {
        JournalEvent event = new JournalEvent();
        event.setLevel(level);
        event.setTime(System.currentTimeMillis());
        event.setName(name);
        event.setItem(entity);
        getHandler().handle(event);
    }
}
